// pages/search/search.js
 import { search } from '../../utils/url.js';
// import defaultData from '../../data.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      i:0,
      curIndex:0,
      upImg:'up-gray',
      downImg:'down-gray',
      goodsName:'',
      searchNum:"1",
      footerTips:true,
      suggestView:'hidden',
  },
//   点击搜索栏出现推荐搜索
//   bindsuggest:function(e){
//       let that = this;
//       wx.request({
//           url: search.searchTxt,
//           method: 'GET',
//           header: {
//               'Accept': 'application/json'
//           },
//           success: function (res) {
//               console.log(res)
//               that.setData({
//                   suggestView:'',
//                   searchTxt: res.data.searchTxt
//               })
//           }
//       })
//   },
//   点击推荐搜索内容
    // suggestSearch:function(e){
    //     console.log(e)
    //     this.setData({
    //         goodsName:e.currentTarget.dataset.suggest,
    //         curIndex: 0
    //     })
    //     let that = this,
    //         goodsName = that.data.goodsName,
    //         searchNum = that.data.searchNum;
    //     // 搜索请求
    //     wx.request({
    //         url: search.goods,
    //         method: 'get',
    //         data: {
    //             searchNum: searchNum,
    //             goodsName: goodsName,
    //         },
    //         header: {
    //             'Accept': 'application/json'
    //         },
    //         success: function (res) {
    //             console.log(res.data)
    //             if (res.data.return_code) {
    //                 that.setData({
    //                     adviceItems: res.data.result.list,
    //                     suggestView:'hidden'
    //                 })
    //                 console.log(that.data.goodsName)
    //             } else {
    //                 wx.request({
    //                     url: search.goods,
    //                     method: 'get',
    //                     data: {
    //                         searchNum: searchNum,
    //                         goodsName: '',//未找到结果 以空字符串查询为你推荐
    //                     },
    //                     header: {
    //                         'Accept': 'application/json'
    //                     },
    //                     success: function (res) {
    //                         that.setData({
    //                             curIndex: 4,
    //                             adviceItems: res.data.result.list,
    //                             suggestView: 'hidden'
    //                         })
    //                         console.log(res.data)
    //                     }
    //                 })
    //             }
    //         }
    //     })    
    // },
  bindKeyInput(e){
      this.setData({
          goodsName: e.detail.value,
          curIndex: 0
      })  
    },
  inputBlur:function(e){
       
        console.log(e)
        var that  = this;
        var goodsName = that.data.goodsName;
        var searchNum = that.data.searchNum;
        // 搜索请求
        wx.request({
            url: search.goods,
            method: 'get',
            data: {
                searchNum: searchNum,
                goodsName: goodsName,
            },
            header: {
                'Accept': 'application/json'
            },
            success: function (res) {
                // console.log(res.data)
                if (res.data.result.list.length>0){
                    that.setData({
                        adviceItems: res.data.result.list,
                        suggestView: 'hidden'
                    })
                   
                }else{
                    console.log(searchNum)
                    wx.request({
                        url: search.goods,
                        method: 'get',
                        data: {
                            searchNum: searchNum,
                            goodsName:""
                        },
                        header: {
                            'Accept': 'application/json'
                        },
                        success: function (ress) {
                            console.log(ress)
                            that.setData({
                                curIndex: 4,
                                adviceItems: ress.data.result.list,
                                suggestView: 'hidden',
                            })
                            console.log(res.data)
                        }
                    })
                }
            }
        })
  },
  bindTap(e) {
      var that = this;
      const index = parseInt(e.currentTarget.dataset.index);
      that.setData({
          curIndex: index,
          upImg: 'up-gray',
          downImg: 'down-gray',
      })
      var goodsName = that.data.goodsName;
      var searchNum = "1";
      that.setData({
          searchNum: searchNum
      })
      wx.request({
          url: search.goods,
          method: 'get',
          data: {
              searchNum: searchNum,
              goodsName: goodsName,
              searchType: 'normal',
          },
          header: {
              'Accept': 'application/json'
          },
          success: function (res) {
              if (res.data.result.list.length > 0){
                  that.setData({
                      adviceItems: res.data.result.list
                  })
              }else{
                  wx.request({
                      url: search.goods,
                      method: 'get',
                      data: {
                          searchNum: searchNum,
                          //未找到结果 以空字符串查询为你推荐
                      },
                      header: {
                          'Accept': 'application/json'
                      },
                      success: function (res) {
                          that.setData({
                              curIndex: 4,
                              adviceItems: res.data.result.list
                          })
                          console.log(res.data)
                      }
                  })
              }
                  
              console.log(res.data);
              console.log("这是按综合搜索");
          }
      })
  },
//   按价格搜索
  clickprice:function(e){
      var that = this;
      var goodsName = that.data.goodsName;
      var searchNum = "1";
      const index = parseInt(e.currentTarget.dataset.index);
      that.setData({
          curIndex: index,
          searchNum: searchNum
      })
          var i = that.data.i;
          console.log(i);
          console.log('价格按升序排列');
          if (i % 2 == 0) {
            //   价格按升序排列
              that.setData({
                  upImg: 'up-white',
                  downImg: 'down-gray',
                  i: 1,
              })
                  wx.request({
                      url: search.goods,
                      method: 'get',
                      data: {
                          searchNum: searchNum,
                          goodsName: goodsName,
                          searchType: 'upprice',
                      },
                      header: {
                          'Accept': 'application/json'
                      },
                      success: function (res) {
                          if (res.data.result.list.length > 0) {
                              that.setData({
                                  adviceItems: res.data.result.list
                              })
                          } else {
                              wx.request({
                                  url: search.goods,
                                  method: 'get',
                                  data: {
                                      searchNum: searchNum,
                                      searchType: 'upprice',
                                    //   upImg: 'up-gray',
                                    //   downImg: 'down-gray',
                                     //未找到结果 以空字符串查询为你推荐
                                  },
                                  header: {
                                      'Accept': 'application/json'
                                  },
                                  success: function (res) {
                                      that.setData({
                                          curIndex: 4,
                                          adviceItems: res.data.result.list
                                      })
                                      console.log(res.data)
                                  }
                              })
                          }
                          console.log(res.data)

                      }
                  })
          } else {
              console.log(i);
              console.log('价格按降序排列');
              that.setData({
                  upImg: 'up-gray',
                  downImg: 'down-white',
                  i: 0,
              })
              wx.request({
                  url: search.goods,
                  method: 'get',
                  data: {
                      searchNum: searchNum,
                      goodsName: goodsName,
                      searchType: 'downprice',
                  },
                  header: {
                      'Accept': 'application/json'
                  },
                  success: function (res) {
                      if (res.data.result.list.length > 0) {
                          that.setData({
                              adviceItems: res.data.result.list
                          })
                      } else {
                          wx.request({
                              url: search.goods,
                              method: 'get',
                              data: {
                                  searchNum: searchNum,
                                  searchType: 'downprice',
                                //   upImg: 'up-gray',
                                //   downImg: 'down-gray',
                                 //未找到结果 以空字符串查询为你推荐
                              },
                              header: {
                                  'Accept': 'application/json'
                              },
                              success: function (res) {
                                  that.setData({
                                      curIndex: 4,
                                      adviceItems: res.data.result.list
                                  })
                                  console.log(res.data)
                              }
                          })
                      }
                      console.log(res.data)

                  }
              })
          };
  },
//   这是按销量搜索
  clickSale:function(e){
      var that = this;
      var goodsName = that.data.goodsName;
      var searchNum = "1";
      const index = parseInt(e.currentTarget.dataset.index);
      that.setData({
          curIndex: index,
          upImg: 'up-gray',
          downImg: 'down-gray',
          searchNum: searchNum
      })
      wx.request({
          url: search.goods,
          method: 'get',
          data: {
              searchNum: searchNum,
              goodsName: goodsName,
              searchType: 'sales',
          },
          header: {
              'Accept': 'application/json'
          },
          success: function (res) {
              if (res.data.result.list.length > 0) {
                  that.setData({
                      adviceItems: res.data.result.list
                  })
              } else {
                  wx.request({
                      url: search.goods,
                      method: 'get',
                      data: {
                          searchNum: searchNum,
                          searchType: 'sales',
                         //未找到结果 以空字符串查询为你推荐
                      },
                      header: {
                          'Accept': 'application/json'
                      },
                      success: function (res) {
                          that.setData({
                              curIndex: 4,
                              adviceItems: res.data.result.list
                          })
                          console.log(res.data)
                      }
                  })
              }
              console.log(res.data);
              console.log("这是按销量搜索");
          }
      })
  },
  clickRecommend: function (e) {
      var that = this;
      var goodsName = that.data.goodsName;
      var searchNum = "1";
      const index = parseInt(e.currentTarget.dataset.index);
      that.setData({
          curIndex: index,
          upImg: 'up-gray',
          downImg: 'down-gray',
          searchNum: searchNum
      })
      wx.request({
          url: search.goods,
          method: 'get',
          data: {
              searchNum: searchNum,
              goodsName: goodsName,
              searchType: 'recommend',
          },
          header: {
              'Accept': 'application/json'
          },
          success: function (res) {
              if (res.data.result.list.length > 0) {
                  that.setData({
                      adviceItems: res.data.result.list
                  })
              } else {
                  wx.request({
                      url: search.goods,
                      method: 'get',
                      data: {
                          searchNum: searchNum,
                          searchType: 'sales',
                         //未找到结果 以空字符串查询为你推荐
                      },
                      header: {
                          'Accept': 'application/json'
                      },
                      success: function (res) {
                          that.setData({
                              curIndex: 4,
                              adviceItems: res.data.result.list
                          })
                          console.log(res.data)
                      }
                  })
              }
              console.log(res.data);
              console.log("这是按热度搜索");
          }
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that = this;
      var goodsName = that.data.goodsName;
      var searchNum = that.data.searchNum;
      // 搜索请求
      wx.request({
          url: search.goods,
          method: 'get',
          data: {
              searchNum: searchNum,
              goodsName: goodsName,
          },
          header: {
              'Accept': 'application/json'
          },
          success: function (res) {
              that.setData({
                  adviceItems: res.data.result.list
              })
              console.log(res.data)

          }
      })
  },
//   下拉加载
  onReachBottom: function () {
      var that = this;
      var searchNum = ++that.data.searchNum ;
      var goodsName = that.data.goodsName;
      var adviceItems = that.data.adviceItems
      console.log('--------下拉刷新-------')
      wx.showNavigationBarLoading() //在标题栏中显示加载
      wx.request({
          url: search.goods,
          data: {
              searchNum: searchNum,
              goodsName: goodsName,
          },
          method: 'get',
          success: function (res) {
              for (var i = 0; i < res.data.result.list.length; i++) {
                    adviceItems.push(res.data.result.list[i]);
                  }
                  console.log(res.data.length);
                   
                  that.setData({
                      adviceItems: adviceItems,
                      searchNum: searchNum
                  })
              
              
          },
          fail: function () {
              console.log('呀！传输失败了哦')
          },
          complete: function () {
              wx.hideNavigationBarLoading() //完成停止加载
              wx.stopPullDownRefresh() //停止下拉刷新
          }
      })
  },

})